﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data;
using System.Data.SqlClient;

namespace AlternateProject.Admin.Content
{
    public partial class Default2 : System.Web.UI.Page
    {
        public string APPROOT = "";
        int PageId = 15;

        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication Rua = new Authentication();
            Rua.CheckRolePageAuthentication(PageId, Authentication.RedirectionURLTypes.LoginPage);

            AppPathFinder AppPath = new AppPathFinder();
            APPROOT = AppPath.GetApplicationPath();

            if (!IsPostBack)
            {

                FillCombo fc = new FillCombo();
                fc.LookUpValue(ddlCONTENTTYPE, "", true, "ADM_CONTENT_TYPE", Session["LANGCODE"].ToString());
                fc.LookUpValue(ddlLANGUAGE, "TR", false, "SYS_APP_LANGUAGE", Session["LANGCODE"].ToString());

                this.btnSearch_Click(null, null);
            }
        }

        private DataTable getData()
        {
            SqlCommand cmd = new SqlCommand();
            string sSQL = "";

            sSQL += @" SELECT A.*, LVL.LOOKUPNAME 
                     FROM  (
             	            SELECT C.CONTENTID, C.ADM_CONTENT_TYPE,  
             	            C.CREUSERID, C.CREDATE, C.MODUSERID, C.MODDATE, 
             	            CL.CONTENTNAME, CL.CONTENTDESC, CL.LANGCODE 
             	            FROM TBLCONTENT C, TBLCONTENT_L CL 
             	            WHERE C.CONTENTID = CL.CONTENTID ";
            if (txtCONTENTID.Text.Trim() != "")
            {
                sSQL += " 	AND C.CONTENTID = @CONTENTID";
                cmd.Parameters.Add("@CONTENTID", SqlDbType.Int).Value = txtCONTENTID.Text.Trim();
            }
            if (ddlCONTENTTYPE.SelectedValue != "")
            {
                sSQL += " 	AND C.ADM_CONTENT_TYPE = @ADM_CONTENT_TYPE";
                cmd.Parameters.Add("@ADM_CONTENT_TYPE", SqlDbType.NVarChar).Value = ddlCONTENTTYPE.SelectedValue;
            }
            sSQL += @" 	AND CL.LANGCODE = @LANGCODE 
                     )A, TBLLOOKUPVALUE_L LVL 
                     WHERE LVL.LOOKUPTYPE = 'ADM_CONTENT_TYPE' 
                     AND A.ADM_CONTENT_TYPE = LVL.LOOKUPVALUE 
                     AND A.LANGCODE = LVL.LANGCODE";
            if (txtCONTENTNAME.Text.Trim() != "")
            {
                sSQL += " 	AND A.CONTENTNAME LIKE @CONTENTNAME";
                cmd.Parameters.Add("@CONTENTNAME", SqlDbType.NVarChar).Value = "%" + txtCONTENTNAME.Text.Trim() + "%";
            }
            sSQL += " ORDER BY CONTENTNAME";


            cmd.CommandText = sSQL;
            cmd.Parameters.Add("@LANGCODE", SqlDbType.NVarChar).Value = ddlLANGUAGE.SelectedValue;
            DbHelper dsrv = new DbHelper();
            DataTable dt = dsrv.GetDataTable(cmd, "tbl");

            return dt;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            rpt.DataSource = this.getData();
            rpt.DataBind();
        }
    }
}